package com.wt.controller;

import com.wt.entity.WqunAccount;
import com.wt.entity.utilEntity.login.LoginAccount;
import com.wt.service.WqunAccountService;
import com.wt.service.impl.WqunAccountServiceImpl;
import com.wt.util.R.R;
import com.wt.util.ResultMess.ResultMess;
import com.wt.util.oos.Upload;

public class WqunAccountController {

    WqunAccountService wqunAccountService = new WqunAccountServiceImpl();

    public R findAccountPublicById(int id){

        WqunAccount findAccountPublicById = wqunAccountService.getOneById(id);
        if(findAccountPublicById!=null){
            findAccountPublicById.setPhone(null);
            findAccountPublicById.setPass(null);
            findAccountPublicById.setSalt(null);
            return R.ok().data("findAccountPublicById",findAccountPublicById);
        }else{
            return R.error().message("无此用户");
        }
    }

    public R deleteById(int id){

        boolean resDelete = wqunAccountService.deleteById(id);
        return resDelete?R.ok().message("删除成功"):R.error().message("删除失败");
    }


    public R findAccountPage(int pageSize,int currentPage,String selectValue,String inputValue){

        return wqunAccountService.findAccountPage(pageSize,currentPage,selectValue,inputValue);
    }


    public R updatePass(WqunAccount wqunAccount){

        boolean UpdateRes = wqunAccountService.updatePass(wqunAccount);
        if(UpdateRes){
            return R.ok().message(ResultMess.UPDATE_USER_SUCCESS.getDesc());
        }else{
            return R.error().message(ResultMess.UPDATE_USER_ERROR.getDesc());
        }
    }

    public R update(WqunAccount wqunAccount){

        R resByPhone = wqunAccountService.selectAccountByPhone(wqunAccount.getPhone());
        WqunAccount selectAccountByPhone = (WqunAccount) resByPhone.getData().get("selectAccountByPhone");
        boolean deleteImgRes = true;
        if(selectAccountByPhone.getAvatar() != null && selectAccountByPhone.getAvatar().length() != 0 && !selectAccountByPhone.getAvatar().equals(wqunAccount.getAvatar())){
            deleteImgRes = Upload.deleteUploadImg(selectAccountByPhone.getAvatar());
        }
        if(!deleteImgRes){
            return R.error().message(ResultMess.UPDATE_USER_ERROR.getDesc());
        }
        boolean UpdateRes = wqunAccountService.update(wqunAccount);
        if(UpdateRes){
            return R.ok().message(ResultMess.UPDATE_USER_SUCCESS.getDesc());
        }else{
            return R.error().message(ResultMess.UPDATE_USER_ERROR.getDesc());
        }
    }
}
